﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.ComponentModel;
using System.Drawing.Drawing2D;
using HospitalSystem.Core;

namespace Village_Project
{
    public partial class regfamily : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["CID"] == null)
            {
                MessageBoxurl("กรุณากดค้นหา หรือลงทะเบียนเด็กก่อน", "findchild.aspx");
            }
            else
            {

                string fCIDstring = Request.QueryString["fCID"];
                string CIDstring = Request.QueryString["CID"];

                //ไว้โชว์ข้อมูลครอบครัวเช่น  regfamily.aspx?CID=1669800114444&fCID=11 จะเด้งมาหน้าล่าง
                if (CIDstring != null && fCIDstring != null)
                {
                    if (!IsPostBack)
                    {
                        dropdownRelation();
                        showlistfamilylb();
                        Panel2.Visible = false;
                        Panel3.Visible = false;
                        Panel1.Visible = false;
                        WelfareRegPanel.Visible = true;
                        CensusRegPanel.Visible = true;
                        LiveRegPanel.Visible = true;
                        checkcarerinfo();
                        show_Images();
                        showsession();
                        loaddatadropdownProvince();
                        loaddatadropdownProvince2();
                        loaddatadropdownTitles();
                        loaddataOccupationRBL();
                        loaddataEducationLevelRBL();
                        loaddataMariageStatusRBL();
                        loaddataLiveStatusRBL();
                    }

                }
                //หน้าต่างปกติ
                else
                {

                    if (!IsPostBack)
                    {
                        dropdownRelation();

                        show_Images();
                        showsession();
                        checkcarerinfo();
                        loaddatadropdownProvince();
                        loaddatadropdownProvince2();
                        loaddatadropdownTitles();
                        loaddataOccupationRBL();
                        loaddataEducationLevelRBL();
                        loaddataMariageStatusRBL();
                        loaddataLiveStatusRBL();

                    }
                }

                //checkinfo();


            }

        }


        protected void showsession()
        {
            String ShowValue = (string)Session["CID"];
            CIDLBL.Text = ShowValue;
            String ShowValue2 = (string)Session["LastName"];
            LName.Text = ShowValue2;

            String ShowValue3 = (string)Session["FirstName"];
            FName.Text = ShowValue3;

            

        }


        public void show_Images()
        {
            string sj = "";



            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT Child_Pic FROM [Person] where CID=@CID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", Session["CID"]);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();

            if (reader2.Read())
            {
                sj = reader2["Child_Pic"].ToString();
            }
            else
            {
                sj = "~/pic/profile/nopic.jpg";
            }
            conn.Close();
            reader2.Close();


            Image1.ImageUrl = (sj);
        }







        protected void AddressHomeCheckbox_CheckedChanged(object sender, EventArgs e)
        {
        }

        protected void WelfareEdit_Button_Click(object sender, EventArgs e)
        {
            Panel2.Visible = true;
            Panel3.Visible = true;
            Panel1.Visible = true;
            Panel4.Visible = true;
            WelfareRegPanel.Visible = false;
            CensusRegPanel.Visible = false;
            LiveRegPanel.Visible = false;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            insertdata();
            WelfareEdit_Button.Visible = true;
            Response.Redirect("regfamily.aspx");

        }




        public void dropdownRelation()
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);
            //LiveStatus

            SqlCommand CmdSql2 = new SqlCommand("SELECT RelationThaiDescription,RelationID FROM [Relation]", conn);
            conn.Open();



            SqlDataReader reader4 = CmdSql2.ExecuteReader();

            DataTable dt = new DataTable();
            MariageStatus.Items.Clear();
            ListItem newItem = new ListItem();
            newItem.Text = "เลือกความสัมพันธ์";
            newItem.Value = "0";
            MariageStatus.Items.Add(newItem);

            while (reader4.Read())
            {
                newItem = new ListItem();
                newItem.Text = reader4["RelationThaiDescription"].ToString();
                newItem.Value = reader4["RelationID"].ToString();
                MariageStatus.Items.Add(newItem);
            }


        }

        protected void insertdata()
        {





            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn2 = new SqlConnection(constr);




            SqlCommand CmdSql = new SqlCommand("delete from [Family] where CID=@CID and FMemberCID=@fCID", conn2);
            conn2.Open();
            CmdSql.Parameters.AddWithValue("@CID", Session["CID"]);
            CmdSql.Parameters.AddWithValue("@fCID", CIDTB.Text);
            CmdSql.ExecuteNonQuery();
            conn2.Close();












            SqlCommand CmdSql2 = new SqlCommand("INSERT INTO [Family] (CID,Title,Relation,FirstName,LastName,FMemberCID,Race,Religion,LiveStatus,LiveHouseNumber,LiveVillageName,LiveMooNumber,LiveAlley,LiveStreetName,LiveTumbon,LiveCity,LiveProvince,LivePostCode,CensusHouseNumber,CensusVillageName,CensusMooNumber,CensusAlley,CensusStreetName,CensusTumbon,CensusCity,CensusProvince,CensusPostCode,LiveMoveOutReason,CensusMoveOutReason,Nation,MariageStatus,EducationLevel,Occupation,AddressOccupation,IncomePerMonth,OtherIncome,OtherIncomeFrom,LiveMoveInDate,LiveMoveOutDate,CensusMoveInDate,CensusMoveOutDate,DateTimeUpdate) VALUES (@CID,@Title,@Relation,@Name,@LastName,@fCID,@Race,@Religion,@LiveStatus,@LiveHouseNumber,@LiveHouseName,@LiveHouseMoo,@LiveHousesoy,@LiveStreetName,@Livetambon,@LiveCity,@LiveProvince,@LivePostcode,@CensusHouseNumber,@CensusHouseName,@CensusHouseMoo,@CensusHousesoy,@CensusStreetName,@Censustambon,@CensusCity,@CensusProvince,@CensusPostcode,@LiveMoveOutReason,@CensusMoveOutReason,@Nation,@MariageStatus,@EducationLevel,@Occupation,@AddressOccupation,@IncomePerMonth,@OtherIncome,@OtherIncomeFrom,@LiveMoveInDate,@LiveMoveOutDate,@CensusMoveInDate,@CensusMoveOutDate,@DateTimeUpdate)", conn2);
            //SqlCommand CmdSql2 = new SqlCommand("INSERT INTO [Family] (CID,MariageStatus,Name,LastName,fCID,Race,Religion,LiveStatus,LiveHouseNumber,LiveHouseName,LiveHouseMoo,LiveHousesoy,LiveStreetName,Livetambon,LiveCity,LiveProvince,LivePostcode,CensusHouseNumber,CensusHouseName,CensusHouseMoo,CensusHousesoy,CensusStreetName,Censustambon,CensusCity,CensusProvince,CensusPostcode,IncomePerMonth,EduLevel,AddressOccupation,Occupation,OherIncome,OherIncomeFrom) VALUES (@CID,@MariageStatus,@Name,@LastName,@fCID,@Race,@Religion,@LiveStatus,@LiveHouseNumber,@LiveHouseName,@LiveHouseMoo,@LiveHousesoy,@LiveStreetName,@Livetambon,@LiveCity,@LiveProvince,@LivePostcode,@CensusHouseNumber,@CensusHouseName,@CensusHouseMoo,@CensusHousesoy,@CensusStreetName,@Censustambon,@CensusCity,@CensusProvince,@CensusPostcode,@IncomePerMonth,@EduLevel,@AddressOccupation,@Occupation,@OherIncome,@OherIncomeFrom)", conn2);
            conn2.Open();
            CmdSql2.Parameters.AddWithValue("@CID", Session["CID"]);

            CmdSql2.Parameters.AddWithValue("@Title", TitlesD.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@Relation", MariageStatus.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@Name", FirstName.Text);
            CmdSql2.Parameters.AddWithValue("@LastName", LastName.Text);
            CmdSql2.Parameters.AddWithValue("@fCID", CIDTB.Text);
            CmdSql2.Parameters.AddWithValue("@Nation", NationTB.Text);
            CmdSql2.Parameters.AddWithValue("@Race", RaceTB.Text);
            CmdSql2.Parameters.AddWithValue("@Religion", ReligionTB.Text);
            CmdSql2.Parameters.AddWithValue("@LiveStatus", LiveStatusRBL.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@MariageStatus", MariageStatusRBL.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@LiveHouseNumber", LHomeTB.Text);
            CmdSql2.Parameters.AddWithValue("@LiveHouseName", LHomeNTB.Text);
            CmdSql2.Parameters.AddWithValue("@LiveHouseMoo", LGroupTB.Text);
            CmdSql2.Parameters.AddWithValue("@LiveHousesoy", LSoyTB.Text);
            CmdSql2.Parameters.AddWithValue("@LiveStreetName", LStreetTB.Text);
            CmdSql2.Parameters.AddWithValue("@Livetambon", LTambonD.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@LiveCity", LCityD.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@LiveProvince", LProvinceD.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@LivePostcode", LPostcodeTB.Text);
            CmdSql2.Parameters.AddWithValue("@CensusHouseNumber", CHomeTB.Text);

            //CmdSql2.Parameters.AddWithValue("@CensusHouseName", CHomeNTB.Text);
            CmdSql2.Parameters.AddWithValue("@CensusHouseName", CHomeNTB.Text);
            CmdSql2.Parameters.AddWithValue("@CensusHouseMoo", CGroupTB.Text);
            CmdSql2.Parameters.AddWithValue("@CensusHousesoy", CSoyTB.Text);
            //CmdSql2.Parameters.AddWithValue("@CensusHousesoy", CHomeTB.Text); ซอย
            CmdSql2.Parameters.AddWithValue("@CensusStreetName", CStreetTB.Text);
            CmdSql2.Parameters.AddWithValue("@Censustambon", CTambonD.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@CensusCity", CCityD.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@CensusProvince", CProvinceD.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@CensusPostCode", CPostcodeTB.Text);
   

            CmdSql2.Parameters.AddWithValue("@EducationLevel", EducationLevelRBL.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@Occupation", OccupationRBL.SelectedValue);

            CmdSql2.Parameters.AddWithValue("@AddressOccupation", AddressOccupationTB.Text);
            CmdSql2.Parameters.AddWithValue("@IncomePerMonth", IncomePerMonthTB.Text);
            CmdSql2.Parameters.AddWithValue("@OtherIncome", OtherIncomeTB.Text);
            CmdSql2.Parameters.AddWithValue("@OtherIncomeFrom", OtherIncomeFromTB.Text);


            CmdSql2.Parameters.AddWithValue("@LiveMoveInDate", LMIDTB.Text);
            CmdSql2.Parameters.AddWithValue("@LiveMoveOutReason", LiveMoveOutReason.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@CensusMoveOutReason", CensusMoveOutReason.SelectedValue);
            CmdSql2.Parameters.AddWithValue("@LiveMoveOutDate", LMODTB.Text);


            CmdSql2.Parameters.AddWithValue("@CensusMoveOutDate", CMODTB.Text);
            CmdSql2.Parameters.AddWithValue("@CensusMoveInDate", CMIDTB.Text);
            CmdSql2.Parameters.AddWithValue("@DateTimeUpdate", DateTime.Now);
            //CmdSql2.Parameters.AddWithValue("@EduLevel", EduLevel.SelectedValue);
            //CmdSql2.Parameters.AddWithValue("@AddressOccupation", AddressOccupation);
            //CmdSql2.Parameters.AddWithValue("@Occupation", Occupation.SelectedValue);
            //CmdSql2.Parameters.AddWithValue("@OherIncome", OherIncome);
            //CmdSql2.Parameters.AddWithValue("@OherIncomeFrom", OherIncomeFrom);
            CmdSql2.ExecuteNonQuery();
            conn2.Close();

        }

        //Wizard1_ActiveStepChanged


        //=================================================================================





        //ยังไม่มีผู้ดูแลหลัก

        public void showlistfamily()
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("select a.FirstName as Name,a.LastName as LastName,a.FMemberCID as fCID,b.RelationThaiDescription as MariageStatus,a.CID as CID from Family a join Relation b on a.Relation=b.RelationID where CID=@CID", scon);
            cmd.Parameters.AddWithValue("@CID", Session["CID"]);
            scon.Open();
            //SqlDataReader reader = cmd.ExecuteReader();

            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            familylist.DataSource = ds;
            familylist.DataBind();
            scon.Close();

            //familylist.DataSource = reader;
            //familylist.DataBind();





            //reader.Close();

        }





        //มีผู้ดูแลหลักแล้ว
        public void showlistfamily2()
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("select a.FirstName as Name,a.LastName as LastName,a.FMemberCID as fCID,b.RelationThaiDescription as MariageStatus,a.CID as CID from Family a join Relation b on a.Relation=b.RelationID where CID=@CID", scon);
            cmd.Parameters.AddWithValue("@CID", Session["CID"]);
            scon.Open();
            //SqlDataReader reader = cmd.ExecuteReader();

            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            familylist0.DataSource = ds;
            familylist0.DataBind();
            scon.Close();

            //familylist.DataSource = reader;
            //familylist.DataBind();





            //reader.Close();

        }




        public void showlistfamily3()
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("select a.FirstName as Name,a.LastName as LastName,a.FMemberCID as fCID,b.RelationThaiDescription as MariageStatus,a.CID as CID from Family a join Relation b on a.Relation=b.RelationID where a.CID=@CID and a.CarerStatus='1'", scon);
            cmd.Parameters.AddWithValue("@CID", Session["CID"]);
            scon.Open();
            //SqlDataReader reader = cmd.ExecuteReader();

            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            familylist1.DataSource = ds;
            familylist1.DataBind();
            scon.Close();

            //familylist.DataSource = reader;
            //familylist.DataBind();





            //reader.Close();

        }



        protected void familylist_SelectedIndexChanged(object sender, GridViewPageEventArgs e)
        {
            familylist.PageIndex = e.NewPageIndex;
            showlistfamily();
        }






        public void showlistfamilylb()
        {

            string fCIDstring = Request.QueryString["fCID"];
            string CIDstring = Request.QueryString["CID"];
            if (CIDstring != null && fCIDstring != null)
            {
                string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
                SqlConnection scon = new SqlConnection(constr);
                SqlCommand cmd = new SqlCommand("select MariageStatus,LiveStatus,Title,EducationLevel,Occupation,AddressOccupation,IncomePerMonth,OtherIncome,OtherIncomeFrom,Relation,FMemberCID,CID,Title,FirstName,LastName,Nation,Race,Religion,LiveHouseNumber,LiveMooNumber,LiveVillageName,LiveAlley,LiveStreetName,LiveTumbon,LiveCity,LiveProvince,LivePostCode,LiveMoveInDate,LiveMoveOutDate,LiveMoveOutReason,CensusHouseNumber,CensusMooNumber,CensusVillageName,CensusAlley,CensusStreetName,CensusTumbon,CensusCity,CensusProvince,CensusPostCode,CensusMoveInDate,CensusMoveOutDate,CensusMoveOutReason,DateTimeUpdate FROM [Family] where CID=@CID and FMemberCID=@fCID", scon);
                cmd.Parameters.AddWithValue("@CID", CIDstring);
                cmd.Parameters.AddWithValue("@fCID", fCIDstring);
                scon.Open();
                SqlDataReader reader2 = cmd.ExecuteReader();


                if (reader2.Read())
                {
          
                    RFName.Text = reader2["FirstName"].ToString();
                    RLName.Text = reader2["LastName"].ToString();


                    RCIDLBL.Text = reader2["CID"].ToString();
                  
                    NationLB.Text = reader2["Nation"].ToString();
                    RaceLB.Text = reader2["Race"].ToString();
                    ReligionLB.Text = reader2["Religion"].ToString();

                  
                    LHomeLB.Text = reader2["LiveHouseNumber"].ToString();
                    LStreetLB.Text = reader2["LiveStreetName"].ToString();
                   
                    //LCityLB.Text = reader2["LiveCity"].ToString();
                    //LProvinceLB.Text = reader2["LiveProvince"].ToString();
                    LPostcodeLB.Text = reader2["LivePostCode"].ToString();
                    CHomeLB.Text = reader2["CensusHouseNumber"].ToString();
                    CStreetLB.Text = reader2["CensusStreetName"].ToString();
                 
                    //CCityLB.Text = reader2["CensusCity"].ToString();
                    joinLiveProvince();
                 joinCensusProvince();
                    joinCensusCity();
                    joinLiveCity();
                    joinLiveTumbon();
                    joinCensusTumbon();
                    joinEducationLevel();
joinOccupation();

AddressOccupationLB.Text = reader2["AddressOccupation"].ToString();
IncomePerMonthLB.Text = reader2["IncomePerMonth"].ToString();
OtherIncomeLB.Text = reader2["OtherIncome"].ToString();
OtherIncomeFromLB.Text = reader2["OtherIncomeFrom"].ToString();


                    //CProvinceLB.Text = reader2["CensusProvince"].ToString();
                    CPostcodeLB.Text = reader2["CensusPostcode"].ToString();
                  
                    //RecordedStaffID.Text = reader2["RecordedStaffID"].ToString();

                    ////LiveMoveInDate,LiveMoveOutDate,CensusMoveInDate,CensusMoveOutDate  LiveMoveOutReason,CensusMoveOutReason
                 
                    LMIDLB.Text = reader2["LiveMoveInDate"].ToString();
                    LMODLB.Text = reader2["LiveMoveOutDate"].ToString();
                    CMIDLB.Text = reader2["CensusMoveInDate"].ToString();
                    CMOBLB.Text = reader2["CensusMoveOutDate"].ToString();



                    CGroupLB.Text = reader2["CensusMooNumber"].ToString();
                    CVillageLB.Text = reader2["CensusVillageName"].ToString();
                    CSoyLB.Text = reader2["CensusAlley"].ToString();
                    LGroupLB.Text = reader2["LiveMooNumber"].ToString();
                    LVillageLB.Text = reader2["LiveVillageName"].ToString();
                    LSoyLB.Text = reader2["LiveAlley"].ToString();
               
                }
                else
                {

                }
                scon.Close();
                reader2.Close();
            }
        }

        protected void RegWF_Button_Click(object sender, EventArgs e)
        {
            showlistfamilytb();
            Panel2.Visible = true;
            Panel3.Visible = true;
            Panel1.Visible = true;
            Panel4.Visible = true;
            WelfareRegPanel.Visible = false;
            CensusRegPanel.Visible = false;
            LiveRegPanel.Visible = false;
            WelfareEdit_Button.Visible = false;
            Panel5.Visible =  false;
        }







        //เช็คผู้ดูแลหลักว่ามีไหม
        protected void checkcarerinfo()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT FMemberCID FROM [Family] where CID=@CID and CarerStatus='1'", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", Session["CID"]);


            SqlDataReader reader2 = CmdSql2.ExecuteReader();



            if (reader2.Read())
            {
                //ถ้ามีไปหน้านี้
                showlistfamily2();
                showlistfamily3();
                dele_carer.Visible = true;

            }
            else
            {
                //ถ้าไม่มีไปหน้านี้
                showlistfamily();
            }
            conn.Close();
            reader2.Close();

        }











        public void showlistfamilytb()
        {

            string fCIDstring = Request.QueryString["fCID"];
            string CIDstring = Request.QueryString["CID"];
            if (CIDstring != null && fCIDstring != null)
            {
                string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
                SqlConnection scon = new SqlConnection(constr);
                SqlCommand cmd = new SqlCommand("select MariageStatus,LiveStatus,Title,EducationLevel,Occupation,AddressOccupation,IncomePerMonth,OtherIncome,OtherIncomeFrom,Relation,FMemberCID,CID,Title,FirstName,LastName,Nation,Race,Religion,LiveHouseNumber,LiveMooNumber,LiveVillageName,LiveAlley,LiveStreetName,LiveTumbon,LiveCity,LiveProvince,LivePostCode,LiveMoveInDate,LiveMoveOutDate,LiveMoveOutReason,CensusHouseNumber,CensusMooNumber,CensusVillageName,CensusAlley,CensusStreetName,CensusTumbon,CensusCity,CensusProvince,CensusPostCode,CensusMoveInDate,CensusMoveOutDate,CensusMoveOutReason,DateTimeUpdate FROM [Family] where CID=@CID and FMemberCID=@fCID", scon);
                cmd.Parameters.AddWithValue("@CID", CIDstring);
                cmd.Parameters.AddWithValue("@fCID", fCIDstring);
                scon.Open();
                SqlDataReader reader2 = cmd.ExecuteReader();


                if (reader2.Read())
                {

                    //EducationLevel,Occupation,,
                    TitlesD.SelectedValue = reader2["Title"].ToString();
                    MariageStatusRBL.SelectedValue = reader2["MariageStatus"].ToString();
                    LiveStatusRBL.SelectedValue = reader2["LiveStatus"].ToString();
                    OccupationRBL.SelectedValue = reader2["Occupation"].ToString();
                    EducationLevelRBL.SelectedValue = reader2["EducationLevel"].ToString();
                    AddressOccupationTB.Text = reader2["AddressOccupation"].ToString();
                    IncomePerMonthTB.Text = reader2["IncomePerMonth"].ToString();
                    OtherIncomeTB.Text = reader2["OtherIncome"].ToString();
                    OtherIncomeFromTB.Text = reader2["OtherIncomeFrom"].ToString();


                    FirstName.Text = reader2["FirstName"].ToString();
                    LastName.Text = reader2["LastName"].ToString();
                    CIDTB.Text = reader2["FMemberCID"].ToString();
                    MariageStatus.SelectedValue = reader2["Relation"].ToString();
                    NationTB.Text = reader2["Nation"].ToString();
                    RaceTB.Text = reader2["Race"].ToString();
                    ReligionTB.Text = reader2["Religion"].ToString();
                   
                    LHomeTB.Text = reader2["LiveHouseNumber"].ToString();
                    LStreetTB.Text = reader2["LiveStreetName"].ToString();
                    LGroupTB.Text = reader2["LiveMooNumber"].ToString();
                    LPostcodeTB.Text = reader2["LivePostCode"].ToString();
                    LHomeNTB.Text = reader2["LiveVillageName"].ToString();
                    LSoyTB.Text = reader2["LiveAlley"].ToString();



                    CHomeTB.Text = reader2["CensusHouseNumber"].ToString();
                    CStreetTB.Text = reader2["CensusStreetName"].ToString();
                    CGroupTB.Text = reader2["CensusMooNumber"].ToString();
                    CHomeNTB.Text = reader2["CensusVillageName"].ToString();
                    CSoyTB.Text = reader2["CensusAlley"].ToString();


                    LProvinceD.SelectedValue = reader2["LiveProvince"].ToString();
                    loaddatadropdownLCity();
                    LCityD.SelectedValue = reader2["LiveCity"].ToString();
                    loaddatadropdownLTambon();
                    LTambonD.SelectedValue = reader2["LiveTumbon"].ToString();



                



                    //ต้องสร้าง dropdownlist ก่อนเรียกโหลดค่า
                    CProvinceD.SelectedValue = reader2["CensusProvince"].ToString();
                    loaddatadropdownCCity();
                    CCityD.SelectedValue = reader2["CensusCity"].ToString();
                    loaddatadropdownCTambon();
                    CTambonD.SelectedValue = reader2["Censustumbon"].ToString();



                    CPostcodeTB.Text = reader2["CensusPostCode"].ToString();
      
                    //RecordedStaffID.Text = reader2["RecordedStaffID"].ToString();

                    ////LiveMoveInDate,LiveMoveOutDate,CensusMoveInDate,CensusMoveOutDate  LiveMoveOutReason,CensusMoveOutReason
                    LiveMoveOutReason.SelectedValue = reader2["LiveMoveOutReason"].ToString();
                    CensusMoveOutReason.SelectedValue = reader2["CensusMoveOutReason"].ToString();

                    LMIDTB.Text = reader2["LiveMoveInDate"].ToString();
                    LMODTB.Text = reader2["LiveMoveOutDate"].ToString();
                    CMIDTB.Text = reader2["CensusMoveInDate"].ToString();
                    CMODTB.Text = reader2["CensusMoveOutDate"].ToString();

                }
                else
                {

                }
                scon.Close();
                reader2.Close();
            }
        }

        protected void dele_carer_Click(object sender, EventArgs e)
        {
            deletecarer();

            Response.Redirect("delcarer.aspx");
        }


        private void MessageBoxurl(string msg, string urld)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "');" + "window.location.href='" + urld + "';" + "</script>";
            Page.Controls.Add(lbl);
        }
        public void deletecarer()
        {


            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql = new SqlCommand("UPDATE [Family] SET CarerStatus='0' where CID=@CID and CarerStatus='1'", conn);
            conn.Open();
            CmdSql.Parameters.AddWithValue("@CID", Session["CID"]);

            CmdSql.ExecuteNonQuery();

            conn.Close();
        }







        public void loaddatadropdownProvince()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT ProvinceID,ProvinceDescription FROM Province", conn);
            conn.Open();

            SqlDataReader reader4 = CmdSql2.ExecuteReader();

            DataTable dt = new DataTable();
            CProvinceD.Items.Clear();
            ListItem newItem = new ListItem();
            newItem.Text = "เลือกจังหวัด";
            newItem.Value = "0";
            CProvinceD.Items.Add(newItem);

            while (reader4.Read())
            {
                newItem = new ListItem();
                newItem.Text = reader4["ProvinceDescription"].ToString();
                newItem.Value = reader4["ProvinceID"].ToString();
                CProvinceD.Items.Add(newItem);
            }

        }

        public void loaddatadropdownProvince2()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT ProvinceID,ProvinceDescription FROM Province", conn);
            conn.Open();

            SqlDataReader reader4 = CmdSql2.ExecuteReader();

            DataTable dt = new DataTable();
            LProvinceD.Items.Clear();
            ListItem newItem = new ListItem();
            newItem.Text = "เลือกจังหวัด";
            newItem.Value = "0";
            LProvinceD.Items.Add(newItem);

            while (reader4.Read())
            {
                newItem = new ListItem();
                newItem.Text = reader4["ProvinceDescription"].ToString();
                newItem.Value = reader4["ProvinceID"].ToString();
                LProvinceD.Items.Add(newItem);
            }

        }









        public void loaddatadropdownCCity()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT CityID,CityDescription FROM City where ProvinceID=@ProvinceID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@ProvinceID", CProvinceD.SelectedValue);

            SqlDataReader reader4 = CmdSql2.ExecuteReader();

            DataTable dtCCityD = new DataTable();
            CCityD.Items.Clear();
            ListItem newItem = new ListItem();
            newItem.Text = "เลือกอำเภอ";
            newItem.Value = "0";
            CCityD.Items.Add(newItem);

            while (reader4.Read())
            {
                newItem = new ListItem();
                newItem.Text = reader4["CityDescription"].ToString();
                newItem.Value = reader4["CityID"].ToString();
                CCityD.Items.Add(newItem);
            }

        }


        public void loaddatadropdownLCity()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT CityID,CityDescription FROM City where ProvinceID=@ProvinceID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@ProvinceID", LProvinceD.SelectedValue);

            SqlDataReader reader4 = CmdSql2.ExecuteReader();

            DataTable dtLCityD = new DataTable();
            LCityD.Items.Clear();
            ListItem newItem = new ListItem();
            newItem.Text = "เลือกอำเภอ";
            newItem.Value = "0";
            LCityD.Items.Add(newItem);

            while (reader4.Read())
            {
                newItem = new ListItem();
                newItem.Text = reader4["CityDescription"].ToString();
                newItem.Value = reader4["CityID"].ToString();
                LCityD.Items.Add(newItem);
            }

        }





        public void loaddatadropdownCTambon()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT TumbonID,TumbonDescription FROM Tumbon where CityID=@CityID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CityID", CCityD.SelectedValue);

            SqlDataReader reader4 = CmdSql2.ExecuteReader();

            DataTable dtCTambon = new DataTable();
            CTambonD.Items.Clear();
            ListItem newItem = new ListItem();
            newItem.Text = "เลือกตำบล";
            newItem.Value = "0";
            CTambonD.Items.Add(newItem);

            while (reader4.Read())
            {
                newItem = new ListItem();
                newItem.Text = reader4["TumbonDescription"].ToString();
                newItem.Value = reader4["TumbonID"].ToString();
                CTambonD.Items.Add(newItem);
            }

        }




        public void loaddatadropdownLTambon()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT TumbonID,TumbonDescription FROM Tumbon where CityID=@CityID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CityID", LCityD.SelectedValue);

            SqlDataReader reader4 = CmdSql2.ExecuteReader();

            DataTable dtLTambon = new DataTable();
            LTambonD.Items.Clear();
            ListItem newItem = new ListItem();
            newItem.Text = "เลือกตำบล";
            newItem.Value = "0";
            LTambonD.Items.Add(newItem);

            while (reader4.Read())
            {
                newItem = new ListItem();
                newItem.Text = reader4["TumbonDescription"].ToString();
                newItem.Value = reader4["TumbonID"].ToString();
                LTambonD.Items.Add(newItem);
            }

        }



        protected void CProvinceD_SelectedIndexChanged(object sender, EventArgs e)
        {
            loaddatadropdownCCity();
        }

        protected void CCityD_SelectedIndexChanged(object sender, EventArgs e)
        {
            loaddatadropdownCTambon();
        }

        protected void LProvinceD_SelectedIndexChanged(object sender, EventArgs e)
        {
            loaddatadropdownLCity();
        }

        protected void LCityD_SelectedIndexChanged(object sender, EventArgs e)
        {
            loaddatadropdownLTambon();
        }


        public void loaddatadropdownTitles()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT TitleID,Description FROM Title", conn);
            conn.Open();

            SqlDataReader reader4 = CmdSql2.ExecuteReader();

            DataTable dtt = new DataTable();
            TitlesD.Items.Clear();
            ListItem newItem = new ListItem();


            while (reader4.Read())
            {
                newItem = new ListItem();
                newItem.Text = reader4["Description"].ToString();
                newItem.Value = reader4["TitleID"].ToString();
                TitlesD.Items.Add(newItem);
            }

        }


        protected void familylist_SelectedIndexChanged2(object sender, GridViewPageEventArgs e)
        {
            familylist0.PageIndex = e.NewPageIndex;
            showlistfamily2();
        }











        public void loaddataEducationLevelRBL()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT EducationLevelID,EducationLevelDescription FROM EducationLevel", conn);
            conn.Open();

            SqlDataReader reader4 = CmdSql2.ExecuteReader();

            DataTable dtBloodTypeRBL = new DataTable();
            EducationLevelRBL.Items.Clear();
            ListItem newItem = new ListItem();


            while (reader4.Read())
            {
                newItem = new ListItem();
                newItem.Text = reader4["EducationLevelDescription"].ToString();
                newItem.Value = reader4["EducationLevelID"].ToString();
                EducationLevelRBL.Items.Add(newItem);
            }

        }




        public void loaddataOccupationRBL()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT OccupationID,OccupationDescription FROM Occupation", conn);
            conn.Open();

            SqlDataReader reader4 = CmdSql2.ExecuteReader();

            DataTable dtBloodTypeRBL = new DataTable();
            OccupationRBL.Items.Clear();
            ListItem newItem = new ListItem();


            while (reader4.Read())
            {
                newItem = new ListItem();
                newItem.Text = reader4["OccupationDescription"].ToString();
                newItem.Value = reader4["OccupationID"].ToString();
                OccupationRBL.Items.Add(newItem);
            }

        }







        public void loaddataLiveStatusRBL()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT LiveStatusID,LiveStatusDescription FROM LiveStatus", conn);
            conn.Open();

            SqlDataReader reader4 = CmdSql2.ExecuteReader();

            DataTable dtBloodTypeRBL = new DataTable();
            LiveStatusRBL.Items.Clear();
            ListItem newItem = new ListItem();


            while (reader4.Read())
            {
                newItem = new ListItem();
                newItem.Text = reader4["LiveStatusDescription"].ToString();
                newItem.Value = reader4["LiveStatusID"].ToString();
                LiveStatusRBL.Items.Add(newItem);
            }

        }




        public void loaddataMariageStatusRBL()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT MariageStatusID,MariageStatusDescription FROM MariageStatus", conn);
            conn.Open();

            SqlDataReader reader4 = CmdSql2.ExecuteReader();

            DataTable dtBloodTypeRBL = new DataTable();
            MariageStatusRBL.Items.Clear();
            ListItem newItem = new ListItem();


            while (reader4.Read())
            {
                newItem = new ListItem();
                newItem.Text = reader4["MariageStatusDescription"].ToString();
                newItem.Value = reader4["MariageStatusID"].ToString();
                MariageStatusRBL.Items.Add(newItem);
            }

        }




      



        protected void CMIDIB_Click(object sender, ImageClickEventArgs e)
        {
            CMIDCal.Visible = true;
        }

        protected void CMODIB_Click(object sender, ImageClickEventArgs e)
        {
            CMODCal.Visible = true;
        }

        protected void LMIDIB_Click(object sender, ImageClickEventArgs e)
        {
            LMIDCal.Visible = true;
        }

        protected void LMODIB_Click(object sender, ImageClickEventArgs e)
        {
            LMODCal.Visible = true;
        }

        protected void LMIDCal_SelectionChanged(object sender, EventArgs e)
        {
            LMIDTB.Text = LMIDCal.SelectedDate.ToShortDateString();
            LMIDCal.Visible = false;
        }

      

        protected void CMIDCal_SelectionChanged(object sender, EventArgs e)
        {
            CMIDTB.Text = CMIDCal.SelectedDate.ToShortDateString();
            CMIDCal.Visible = false;
        }

        protected void CMODCal_SelectionChanged(object sender, EventArgs e)
        {
            CMODTB.Text = CMODCal.SelectedDate.ToShortDateString();
            CMODCal.Visible = false;
        }

        protected void LMODCal_SelectionChanged(object sender, EventArgs e)
        {
            LMODTB.Text = LMODCal.SelectedDate.ToShortDateString();
            LMODCal.Visible = false;
        }











        protected void joinLiveProvince()


        {

            string fCIDstring = Request.QueryString["fCID"];
            string CIDstring = Request.QueryString["CID"];


            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);


            SqlCommand CmdSql2 = new SqlCommand("SELECT  Province.[ProvinceDescription] as LiveProvince  FROM [Province] Province join [Family] Person on Person.LiveProvince=Province.ProvinceID  where Person.CID=@CID and Person.FMemberCID=@fCID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", CIDstring);
            CmdSql2.Parameters.AddWithValue("@fCID", fCIDstring);
            SqlDataReader reader2 = CmdSql2.ExecuteReader();

            if (reader2.Read())
            {
                LProvinceLB.Text = reader2["LiveProvince"].ToString();
            }
            else
            {
                //MessageBox("ไม่พบเลขบัตรครับ");
            }
            conn.Close();
            reader2.Close();


        }



        protected void joinCensusProvince()
        {

            string fCIDstring = Request.QueryString["fCID"];
            string CIDstring = Request.QueryString["CID"];


            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);


            SqlCommand CmdSql2 = new SqlCommand("SELECT  Province.[ProvinceDescription] as CensusProvince  FROM [Province] Province join [Family] Person on Person.CensusProvince=Province.ProvinceID  where Person.CID=@CID and Person.FMemberCID=@fCID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", CIDstring);
            CmdSql2.Parameters.AddWithValue("@fCID", fCIDstring);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();

            if (reader2.Read())
            {
                CProvinceLB.Text = reader2["CensusProvince"].ToString();
            }
            else
            {

            }
            conn.Close();
            reader2.Close();


        }






        protected void joinCensusCity()
        {

            string fCIDstring = Request.QueryString["fCID"];
            string CIDstring = Request.QueryString["CID"];
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);


            SqlCommand CmdSql2 = new SqlCommand("SELECT  Province.[CityDescription] as CensusCity  FROM [City] Province join [Family] Person on Person.CensusCity=Province.CityID  where Person.CID=@CID and Person.FMemberCID=@fCID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", CIDstring);
            CmdSql2.Parameters.AddWithValue("@fCID", fCIDstring);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();

            if (reader2.Read())
            {
                CCityLB.Text = reader2["CensusCity"].ToString();
            }
            else
            {

            }
            conn.Close();
            reader2.Close();


        }

        protected void joinLiveCity()
        {

            string fCIDstring = Request.QueryString["fCID"];
            string CIDstring = Request.QueryString["CID"];
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);


            SqlCommand CmdSql2 = new SqlCommand("SELECT  Province.[CityDescription] as LiveCity  FROM [City] Province join [Family] Person on Person.LiveCity=Province.CityID  where Person.CID=@CID and Person.FMemberCID=@fCID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", CIDstring);
            CmdSql2.Parameters.AddWithValue("@fCID", fCIDstring);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();

            if (reader2.Read())
            {
                LCityLB.Text = reader2["LiveCity"].ToString();
            }
            else
            {

            }
            conn.Close();
            reader2.Close();


        }









        //ตำบล



        protected void joinCensusTumbon()
        {

            string fCIDstring = Request.QueryString["fCID"];
            string CIDstring = Request.QueryString["CID"];
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);


            SqlCommand CmdSql2 = new SqlCommand("SELECT  Province.[TumbonDescription] as CensusCity  FROM [Tumbon] Province join [Family] Person on Person.CensusTumbon=Province.TumbonID  where Person.CID=@CID and Person.FMemberCID=@fCID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", CIDstring);
            CmdSql2.Parameters.AddWithValue("@fCID", fCIDstring);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();

            if (reader2.Read())
            {
                CTambonLB.Text = reader2["CensusCity"].ToString();
            }
            else
            {

            }
            conn.Close();
            reader2.Close();


        }

        protected void joinLiveTumbon()
        {

            string fCIDstring = Request.QueryString["fCID"];
            string CIDstring = Request.QueryString["CID"];
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);


            SqlCommand CmdSql2 = new SqlCommand("SELECT  Province.[TumbonDescription] as LiveCity  FROM [Tumbon] Province join [Family] Person on Person.LiveTumbon=Province.TumbonID  where Person.CID=@CID and Person.FMemberCID=@fCID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", CIDstring);
            CmdSql2.Parameters.AddWithValue("@fCID", fCIDstring);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();

            if (reader2.Read())
            {

                LTambonLB.Text = reader2["LiveCity"].ToString();
            }
            else
            {

            }
            conn.Close();
            reader2.Close();


        }






        protected void joinEducationLevel()
        {

            string fCIDstring = Request.QueryString["fCID"];
            string CIDstring = Request.QueryString["CID"];
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);


            SqlCommand CmdSql2 = new SqlCommand("SELECT  Province.[EducationLevelDescription] as LiveCity  FROM [EducationLevel] Province join [Family] Person on Person.EducationLevel=Province.EducationLevelID  where Person.CID=@CID and Person.FMemberCID=@fCID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", CIDstring);
            CmdSql2.Parameters.AddWithValue("@fCID", fCIDstring);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();

            if (reader2.Read())
            {
                EducationLevelLB.Text = reader2["LiveCity"].ToString();
            }
            else
            {

            }
            conn.Close();
            reader2.Close();


        }




        protected void joinOccupation()
        {

            string fCIDstring = Request.QueryString["fCID"];
            string CIDstring = Request.QueryString["CID"];
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);


            SqlCommand CmdSql2 = new SqlCommand("SELECT  Province.[OccupationDescription] as LiveCity  FROM [Occupation] Province join [Family] Person on Person.Occupation=Province.OccupationID  where Person.CID=@CID and Person.FMemberCID=@fCID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", CIDstring);
            CmdSql2.Parameters.AddWithValue("@fCID", fCIDstring);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();

            if (reader2.Read())
            {

                OccupationLB.Text = reader2["LiveCity"].ToString();
            }
            else
            {

            }
            conn.Close();
            reader2.Close();


        }













    }
}